// VPoint.cpp
//
#include "VPoint.h"

#include "OpenGL/Renderer.h"
#include "OpenGL/Selection.h"
#include "OpenGL/OpenGL.h"

#include <iostream>

#include "MemCheck.h"

// ------------------------------------------------------------------------------------------------
VPoint::VPoint()
{}

// ------------------------------------------------------------------------------------------------
VPoint::VPoint( double x, double y, double z )
{
	_coords.x = x;
	_coords.y = y;
	_coords.z = z;

	_boundingBox.extend( _coords );
	_boundingBox.scale(1.2);
}

// ------------------------------------------------------------------------------------------------
VPoint::VPoint( const OpenGL::Math::dvec3 &p )
: _coords(p)
{
	_boundingBox.extend( p );
	_boundingBox.scale(1.2);
}

// ------------------------------------------------------------------------------------------------
VObject::ObjectType VPoint::type() const		{ return Point; }

// ------------------------------------------------------------------------------------------------
std::vector<OpenGL::Math::dvec3> VPoint::snapPoints() const
{
	std::vector<OpenGL::Math::dvec3> pts;
	pts.push_back(_coords);

	return pts;
}

// ------------------------------------------------------------------------------------------------
OpenGL::Math::dvec3 VPoint::textPosition() const
{
	return _coords;
}

// ------------------------------------------------------------------------------------------------
void VPoint::draw( OpenGL::Renderer *r ) const
{
	OpenGL::Math::dvec3 o = r->origin();

	//glPushAttrib(GL_COLOR_BUFFER_BIT|GL_POINT_BIT);
		glPointSize(9 + (isHilighted() * 3.f) );
		glColor3ub(0,0,0);
		glBegin(GL_POINTS);
			glVertex2dv( OpenGL::Math::value_ptr(_coords-o) );
		glEnd();

		glColor3fv( color().rgba() );
		glPointSize(6 + (isHilighted() * 3.f) );
		glBegin(GL_POINTS);
			glVertex2dv( OpenGL::Math::value_ptr(_coords-o) );
		glEnd();


	//glPopAttrib();	



	//glColor4f(1,1,0,1);
	//if( _hilighted )
	//	glColor4f(1,0,0,1);

	//glBegin( GL_POINTS );
	//	glVertex2dv( OpenGL::Math::value_ptr(_coords-o) );
	//glEnd();
}

// ------------------------------------------------------------------------------------------------
void VPoint::drawForSelection( OpenGL::Renderer *r ) const
{
	OpenGL::Selection::Name name( _id );

	OpenGL::Math::dvec3 o = r->origin();
	glBegin( GL_POINTS );
		glVertex2dv( OpenGL::Math::value_ptr(_coords-o) );
	glEnd();
}

// ------------------------------------------------------------------------------------------------
bool VPoint::intersects( const OpenGL::Math::box3 &b ) const
{
	return b.contains( _coords );
}
